<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/26
 * Time: 14:54
 * 首页模块
 */
namespace app\home\model;

use app\home\model\Base;
use think\Model;
use think\Request;
use think\Log;
use think\Db;
use think\Exception;
use think\Session;

class Index extends Base {

    /**
     * 构造
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取公告列表
     * 详情：根据公告参数id获取指定公告详情
     */
    public function getNoticeM()
    {
        try {
            // 指定公告的ID
            $param = Request::instance()->param();
            $sql = "SELECT `id`,`title`,`content`,`create_time` FROM yc_admin_notice WHERE platform like '%2%' and status = 1 ORDER BY id DESC LIMIT 5";
            if($param['id']){
                $sql = "SELECT `id`,`title`,`content`,`create_time` FROM yc_admin_notice WHERE platform like '%2%' and status = 1 and id = ".$param['id']." LIMIT 1";
            }
            $notice = Db::query($sql);
            return $notice;
        } catch (Exception $e) {
            Log::write('获取公告信息异常：'.$e->getMessage(),'error');
        }
    }

    /**
     * 获取用户的总卡量、当月过期卡量以及已使用量
     */
    public function getCardQuantityM()
    {
        try {
            $sql = "SELECT a.`iccid`,a.`id`,c.`dateexpire`,c.`pkg_used`,c.`globalsimtype` FROM yc_agent_card as a LEFT JOIN yc_card AS c ON a.iccid = c.iccid WHERE a.cid = ".$this->user_info['id'];
            $card = Db::query($sql);
            $return_data['total_card'] = count($card);   // 客户总卡量
            $return_data['expire_card'] = 0;   // 客户当月过期卡片量
            $return_data['total_used'] = 0;    // 客户当月卡片已使用总量
            foreach($card as &$value){
                if(F_check_card_expire($value['dateexpire'])){
                    $return_data['expire_card'] += 1;
                }
                $return_data['total_used'] += $value['pkg_used'];
            }
            $return_data['total_used'] = round($return_data['total_used']/(1024*1024),3).'M';  // 换算成单位M
            if($return_data['total_used'] >= 1024){   // 超过1G的换算成单位G
                $return_data['total_used'] = round($return_data['total_used']/1024,3).'G';
            }
            return $return_data;
        } catch (Exception $e) {
            Log::write('获取用户卡量异常：'.$e->getMessage(),'error');
        }
    }
    
    /**
     * 提交用户反馈的信息
     */
    public function submitFeedM()
    {
        try {
            $data = Request::instance()->post();
            $content = $data['feed'];
            $cid = $this->user_info['id'];
            $image = '';   // 存放图片json字符串
            if(isset($data['img'])){   // 补全地址
                foreach($data['img'] as &$value){
                    $value = 'http://'.$_SERVER['HTTP_HOST'].$value;
                }
                $image = json_encode($data['img']);
            }
            $add_time = time();
            $sql = "INSERT INTO yc_feedback (`content`,`cid`,`image`,`addtime`) VALUES ('".$content."','".$cid."','".$image."','".$add_time."')";
            $result = Db::execute($sql);
            if(!$result){
                return ['code'=>1,'msg'=>'提交信息失败，请稍后尝试'];
            }
            return ['code'=>0,'msg'=>'提交成功！'];
        } catch (Exception $e) {
            Log::write('用户反馈信息异常：'.$e->getMessage(),'error');
        }
    }
    
}